﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sharp3DMath.Strokes;
using Sharp3DMath.Core;
using AI.Fuzzy.Library;
using LRSS;

namespace Demo
{

    public class SketchRecogniseFinishedEventArgs : EventArgs
    {
        public readonly List<RuleResult> RecogniseResults;
        public readonly StrokeFeaturesBase Features;
        public SketchRecogniseFinishedEventArgs(List<RuleResult> results, StrokeFeaturesBase features)
        {
            RecogniseResults = results;
            Features = features;
        }
    }

    public partial class PaintControl : UserControl
    {
       




        private bool drawingTag = false;
        private Stroke stroke = Stroke.Zero;
        private LRSS.FuzzyLogicSystemFactory fuzzylogicSystemFactory;
        private MamdaniFuzzySystem fuzzylogic;
        private Point lastPosition = Point.Empty;
        private Graphics DC;
        private Pen strokePen = new Pen(Color.BlueViolet, 2);
        private List<Stroke> strokes;

        public delegate void SketchRecogniseFinishedEventHandler(object sender, SketchRecogniseFinishedEventArgs e);
        public event SketchRecogniseFinishedEventHandler SketchRecogniseFinished;

        public PaintControl()
        {
            InitializeComponent();
            fuzzylogicSystemFactory = new LRSS.FuzzyLogicSystemFactory();
            fuzzylogic = fuzzylogicSystemFactory.CreateSystem();
            strokes = new List<Stroke>();
        }

        private void PaintControl_MouseUp(object sender, MouseEventArgs e)
        {
            drawingTag = false;
            if (stroke.Points.Count <= 20)
            {
                stroke.Clear();
                return;
            }
            strokes.Add(stroke);
            StrokeFeaturesBase features =  LRSS.FeaturesGenerator.GetFeatures(stroke);
            List<RuleResult> recogniseResult;
            recogniseResult = LRSS.RecogniseMachine.Recognise(features);

            DC.Dispose();

            if (SketchRecogniseFinished != null)
            {
                SketchRecogniseFinished(this, new SketchRecogniseFinishedEventArgs(recogniseResult, features));
            }

        }

        private void PaintControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (drawingTag)
            {
                if (e.X != lastPosition.X || e.Y != lastPosition.Y)
                {
                    stroke.AddActivePoint(new Vector2F(e.X, e.Y));

                    DC.DrawLine(strokePen, lastPosition, e.Location);
                    lastPosition.X = e.X;
                    lastPosition.Y = e.Y;
                }
            }
        }

        private void PaintControl_Paint(object sender, PaintEventArgs e)
        {
            if (strokes == null)
            {
                if (stroke == null)
                {
                    return;
                }
                if (!stroke.Empty)
                {
                    Graphics dc = e.Graphics;
                    GraphicsEx.DrawLines(dc, strokePen, stroke.Points);
                }

            }
            else
            {
                foreach (var item in strokes)
                {
                    Graphics dc = e.Graphics;
                    GraphicsEx.DrawStroke(dc, strokePen, item);
                }
            }
        }

        private void PaintControl_MouseDown(object sender, MouseEventArgs e)
        {
            drawingTag = true;
            if (!stroke.Empty)
            {
                //stroke.Clear();
                stroke = Stroke.Zero;
            }
            stroke.AddActivePoint(new Vector2F(e.X, e.Y));
            lastPosition.X = e.X;
            lastPosition.Y = e.Y;
            DC = CreateGraphics();
        }
    }
}
